﻿using log4net;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Service.DataMaintenance;
using PhysicalExamination5.WebApi.Utility.JWT;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography;
using Newtonsoft.Json;
using PhysicalExamination5.Model.Extensions;

namespace PhysicalExamination5.WebApi.Controllers.DataMaintenance
{
    /// <summary>
    /// 套餐维护
    /// </summary>
    [ApiController]
    [ApiVersion("1.0")]
    [Route("[controller]/v{version:apiVersion}")]
    public class TaoCanWeiHuController : ControllerBase
    {
        private readonly ITaoCanWeiHu _TaoCanWeiHu;
        private static readonly ILog log = LogManager.GetLogger(typeof(KeShiWeiHuController));
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ICustomJWTService _ICustomJWTService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="IKeShiWeiHu"></param>
        /// <param name="ICustomJWTService"></param>
        /// <param name="httpContextAccessor"></param>
        public TaoCanWeiHuController(ITaoCanWeiHu ITaoCanWeiHu, ICustomJWTService ICustomJWTService, IHttpContextAccessor httpContextAccessor)
        {
            _TaoCanWeiHu = ITaoCanWeiHu;
            _ICustomJWTService = ICustomJWTService;
            _httpContextAccessor = httpContextAccessor;
            //配置日志ip地址参数信息
            string ipAddress = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
            log4net.GlobalContext.Properties["localIpAddress"] = ipAddress;
        }

        #region 获取数据
        /// <summary>
        /// 获取套餐主
        /// </summary>
        /// <param name="ChaXunJson"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTaoCanZhuList(string? ChaXunJson)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            GetTaoCanChaXun getTaoCanChaXun = new GetTaoCanChaXun();
            if (!ChaXunJson.IsEmpty())
            {
                getTaoCanChaXun = JsonConvert.DeserializeObject<GetTaoCanChaXun>(ChaXunJson);
            }
            return Ok(_TaoCanWeiHu.GetTaoCanZhuList(getTaoCanChaXun));
        }
        /// <summary>
        /// 获取套餐主详情
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTaoCanZhuEntity([FromQuery] string tid)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_TaoCanWeiHu.GetTaoCanZhuEntity(tid));
        }
        /// <summary>
        /// 获取套餐主下的套餐
        /// </summary>
        /// <param name="TaoCanZhuID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTaoCanList([FromQuery] string TaoCanZhuID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_TaoCanWeiHu.GetTaoCanList(TaoCanZhuID));
        }
        /// <summary>
        ///  获取套餐下的项目
        /// </summary>
        /// <param name="TaoCanID"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [Route("[action]")]
        public IActionResult GetTaoCanXiangMuList([FromQuery] string TaoCanID)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_TaoCanWeiHu.GetTaoCanXiangMuList(TaoCanID));
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存套餐主
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="SaveTaoCanZhu"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult SaveTaoCanZhuData([FromQuery] string? tid, [FromBody] SaveTaoCanZhuHelp SaveTaoCanZhu)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_TaoCanWeiHu.SaveTaoCanZhuData(tid, SaveTaoCanZhu, strYuanQuQianZhui));
        }
        /// <summary>
        /// 删除套餐主
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteTaoCanZhuData([FromQuery] string tid)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_TaoCanWeiHu.DeleteTaoCanZhuData(tid));
        }
        /// <summary>
        /// 套餐保存
        /// </summary>
        /// <param name="TaoCanZhuID"></param>
        /// <param name="saveTaoCan"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult SaveTaoCanData([FromQuery] string TaoCanZhuID, [FromBody] SaveTaoCanHelp saveTaoCan)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_TaoCanWeiHu.SaveTaoCanData(TaoCanZhuID, strYuanQuQianZhui, saveTaoCan));
        }
        /// <summary>
        /// 删除套餐
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        [HttpDelete]
        [Authorize]
        [Route("[action]")]
        public IActionResult DeleteTaoCanData([FromQuery] string tid)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            return Ok(_TaoCanWeiHu.DeleteTaoCanData(tid));
        }
        /// <summary>
        /// 修改套餐下的项目
        /// </summary>
        /// <param name="TaoCanZhuID"></param>
        /// <param name="updateTaoCan"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [Route("[action]")]
        public IActionResult UpdateTaoCanData([FromQuery] string TaoCanZhuID, [FromBody] UpdateTaoCanHelp updateTaoCan)
        {
            log.Debug($@"API:{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var accesToken = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
            var tokenHandler = new JwtSecurityTokenHandler();
            var token = tokenHandler.ReadJwtToken(accesToken);
            var strYuanQuQianZhui = token.Claims.FirstOrDefault(t => t.Type == "YuanQuQianZhui")?.Value;
            return Ok(_TaoCanWeiHu.UpdateTaoCanData(TaoCanZhuID, updateTaoCan));
        }
        #endregion
    }
}
